package model

import (
	"database/sql"
	"prod/pkg/orm"
	"time"
)

type ProdType struct {
	ID       uint32 `gorm:"comment:自增主键;type:serial;primaryKey;not null"`
	Name     string `gorm:"comment:类别名称;size:100;not null;uniqueIndex:UNIQ_IDX_PROD_TYPE_NAME,priority:4"`
	SgId     uint32 `gorm:"comment:品牌ID;type:integer;not null;uniqueIndex:UNIQ_IDX_PROD_TYPE_NAME,priority:1;index:IDX_PROD_TYPE_SG,priority:1;index:IDX_PROD_TYPE_PARENT,priority:1"`
	PStoreId uint32 `gorm:"comment:总部&门店ID;type:integer;default:0;uniqueIndex:UNIQ_IDX_PROD_TYPE_NAME,priority:2;index:IDX_PROD_TYPE_SG,priority:2;index:IDX_PROD_TYPE_PARENT,priority:2"`
	Nature   uint8  `gorm:"comment:类型：1~零售品，2~虚拟品，3~服务;type:smallint;not null"`
	ParentId uint32 `gorm:"comment:父类别ID;type:integer;default:0;uniqueIndex:UNIQ_IDX_PROD_TYPE_NAME,priority:3;index:IDX_PROD_TYPE_PARENT,priority:3"`
	Operator uint32 `gorm:"comment:操作人;type:integer;default:0"`
	// 类别具有统计学意义，重建同名类别时，保留旧ID，以确保下拉框能选中历史同名类别的流水分析数据
	Status    bool      `gorm:"comment:状态，true~有效，false~删除;default:true;not null;index:IDX_PROD_TYPE_SG,priority:3;index:IDX_PROD_TYPE_PARENT,priority:4"`
	CreatedAt time.Time `gorm:"comment:创建时间"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

type Product struct {
	ID          uint64          `gorm:"comment:自增主键;type:bigserial;primaryKey;not null"`
	Name        string          `gorm:"comment:商品名称;size:100;not null;uniqueIndex:UNIQ_IDX_PROD_NAME,priority:3"`
	SgId        uint32          `gorm:"comment:品牌ID;type:integer;not null;uniqueIndex:UNIQ_IDX_PROD_NAME,priority:1;index:IDX_PROD_SG,priority:1"`
	PStoreId    uint32          `gorm:"comment:总部&门店ID;type:integer;default:0;uniqueIndex:UNIQ_IDX_PROD_NAME,priority:2;index:IDX_PROD_SG,priority:2"`
	Nature      uint8           `gorm:"comment:类型：1~零售品，2~虚拟品，3~服务;type:smallint;not null"`
	TypeId      uint32          `gorm:"comment:分类ID;type:integer;not null;index:IDX_PROD_TYPE,priority:1"`
	Phonetic    string          `gorm:"comment:拼音简码;size:10;"`
	TitlePics   orm.StringArray `gorm:"comment:主图列表;type:varchar(100)[]"`
	DetailPics  orm.StringArray `gorm:"comment:明细图片列表;type:varchar(100)[]"`
	Description string          `gorm:"comment:商品描述;type:varchar(300)"`
	Unit        string          `gorm:"comment:商品单位;type:varchar(4)"`
	GroupList   orm.StringArray `gorm:"comment:商品分组列表;type:varchar(10)[]"`
	GroupDetail orm.JSON        `gorm:"comment:商品分组属性;type:jsonb"`
	Operator    uint32          `gorm:"comment:操作人;type:integer;default:0"`
	// 商品删除为绝对意义上删除，不触发重建同名
	Status    sql.NullBool `gorm:"comment:状态，true~有效，null~删除;default:true;uniqueIndex:UNIQ_IDX_PROD_NAME,priority:4;index:IDX_PROD_SG,priority:3;index:IDX_PROD_TYPE,priority:2"`
	CreatedAt time.Time    `gorm:"comment:创建时间"`
	UpdatedAt time.Time    `gorm:"comment:更新时间"`
	// 外部关联
	ProdType ProdType `gorm:"foreignKey:TypeId;references:ID;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT"`
}

type Sku struct {
	ID       uint64          `gorm:"comment:自增主键;type:bigserial;primaryKey;not null"`
	Items    orm.StringArray `gorm:"comment:属性项列表;type:varchar(50)[];not null;uniqueIndex:UNIQ_IDX_SKU_NAME,priority:2"`
	Price    uint32          `gorm:"comment:规格价格;type:integer;not null"`
	Weight   uint64          `gorm:"comment:规格重量;type:bigint;not null;default:0"`
	ProdId   uint64          `gorm:"comment:商品ID;type:bigint;not null;uniqueIndex:UNIQ_IDX_SKU_NAME,priority:1;index:IDX_SKU_PROD,priority:1"`
	SgId     uint32          `gorm:"comment:规格ID;type:integer;not null;index:IDX_SKU_SG,priority:1"`
	Operator uint32          `gorm:"comment:操作人;type:integer;default:0"`
	// 由于编辑规格容易触发重建，同名规格需要恢复ID以确保统计一致性
	Status    bool      `gorm:"comment:状态，true~有效，false~删除;default:true;not null;index:IDX_SKU_SG,priority:2"`
	CreatedAt time.Time `gorm:"comment:创建时间"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
	// 外部关联
	Product Product `gorm:"foreignKey:ProdId;references:ID;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT"`
}

type Group struct {
	ID       uint32          `gorm:"comment:自增主键;type:serial;primaryKey;not null"`
	Name     string          `gorm:"comment:分组名称;size:100;not null;uniqueIndex:UNIQ_IDX_PROD_GROUP_NAME,priority:3"`
	SgId     uint32          `gorm:"comment:品牌ID;type:integer;not null;uniqueIndex:UNIQ_IDX_PROD_GROUP_NAME,priority:1;index:IDX_PROD_GROUP_SG,priority:1"`
	PStoreId uint32          `gorm:"comment:总部&门店ID;type:integer;default:0;uniqueIndex:UNIQ_IDX_PROD_GROUP_NAME,priority:2;index:IDX_PROD_GROUP_SG,priority:2"`
	Nature   uint8           `gorm:"comment:类型：1~零售品，2~虚拟品，3~服务;type:smallint;not null"`
	Items    orm.StringArray `gorm:"comment:属性项列表;type:varchar(50)[];not null"`
	Operator uint32          `gorm:"comment:操作人;type:integer;default:0"`
	// 分组目前不具有统计意义，删除重建引入新ID
	Status    sql.NullBool `gorm:"comment:状态，true~有效，null~删除;default:true;uniqueIndex:UNIQ_IDX_PROD_GROUP_NAME,priority:4;index:IDX_PROD_GROUP_SG,priority:3"`
	CreatedAt time.Time    `gorm:"comment:创建时间"`
	UpdatedAt time.Time    `gorm:"comment:更新时间"`
}

type Menu struct {
	ID             uint32       `gorm:"comment:自增主键;type:serial;primaryKey;not null"`
	Name           string       `gorm:"comment:供货组名称;size:50;not null;uniqueIndex:UNIQ_IDX_MENU_NAME,priority:2"`
	SgId           uint32       `gorm:"comment:品牌ID;type:integer;not null;uniqueIndex:UNIQ_IDX_MENU_NAME,priority:1;index:IDX_PROD_MENU_SG,priority:1"`
	Pic            string       `gorm:"comment:图片;size:100"`
	CanModifyPrice bool         `gorm:"comment:是否允许经销商修改价格;type:bool"`
	Operator       uint32       `gorm:"comment:操作人;type:integer;default:0"`
	Status         sql.NullBool `gorm:"comment:状态，true~有效，null~删除;default:true;uniqueIndex:UNIQ_IDX_MENU_NAME,priority:3;index:IDX_PROD_MENU_SG,priority:2"`
	CreatedAt      time.Time    `gorm:"comment:创建时间"`
	UpdatedAt      time.Time    `gorm:"comment:更新时间"`
}

type MenuProd struct {
	MenuId    uint32    `gorm:"comment:供货组ID;type:integer;not null;primaryKey;index:IDX_MENU_PROD_MENU,priority:1;"`
	ProdId    uint64    `gorm:"comment:商品ID;type:bigint;not null;primaryKey;index:IDX_MENU_PROD_PROD,priority:2"`
	SgId      uint32    `gorm:"comment:品牌ID;type:integer;not null"`
	Operator  uint32    `gorm:"comment:操作人;type:integer;default:0"`
	Status    bool      `gorm:"comment:状态，true~有效，false~删除;default:true;index:IDX_MENU_PROD_MENU,priority:2;index:IDX_MENU_PROD_PROD,priority:2"`
	CreatedAt time.Time `gorm:"comment:创建时间"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

type MenuSku struct {
	MenuId    uint32    `gorm:"comment:供货组ID;type:integer;not null;primaryKey;index:IDX_MENU_SKU_MENU,priority:1;"`
	ProdId    uint64    `gorm:"comment:商品ID;type:bigint;not null;primaryKey;index:IDX_MENU_SKU_PROD,priority:1"`
	SkuId     uint64    `gorm:"comment:规格ID;type:bigint;not null;primaryKey;"`
	Price     uint32    `gorm:"comment:规格价格;type:integer;not null"`
	SgId      uint32    `gorm:"comment:品牌ID;type:integer;not null"`
	Operator  uint32    `gorm:"comment:操作人;type:integer;default:0"`
	Status    bool      `gorm:"comment:状态，true~自定义过价格，false~删除;default:true;index:IDX_MENU_SKU_MENU,priority:2;index:IDX_MENU_SKU_PROD,priority:2"`
	CreatedAt time.Time `gorm:"comment:创建时间"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

type MenuStore struct {
	StoreId   uint32    `gorm:"comment:门店ID;type:integer;not null;primaryKey"`
	MenuId    uint32    `gorm:"comment:供货组ID;type:integer;not null;index:IDX_MENU_STORE,priority:1"`
	SgId      uint32    `gorm:"comment:品牌ID;type:integer;not null"`
	Operator  uint32    `gorm:"comment:操作人;type:integer;default:0"`
	Status    bool      `gorm:"comment:状态，true~有效，false~删除;default:true;index:IDX_MENU_STORE,priority:2"`
	CreatedAt time.Time `gorm:"comment:创建时间"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

type Task struct {
	ID           uint64    `gorm:"comment:自增主键;primaryKey;type:bigserial;not null"`
	Operator     uint32    `gorm:"comment:操作人;type:integer;not null"`
	OperatorName string    `gorm:"comment:操作人姓名;size:50;not null"`
	TaskType     uint8     `gorm:"comment:任务类型，1~零售品导入 2~虚拟品导入 3~服务品导入;not null;index:IDX_PROD_TASK_SG,priority:3"`
	Link         string    `gorm:"comment:文件链接;size:100"`
	Extra        string    `gorm:"comment:备注信息;type:text"`
	SgId         uint32    `gorm:"comment:品牌ID;type:integer;not null;index:IDX_PROD_TASK_SG,priority:1"`
	PStoreId     uint32    `gorm:"comment:总部&门店ID;type:integer;default:0;index:IDX_PROD_TASK_SG,priority:2"`
	Status       uint8     `gorm:"comment:状态，0~进行中，1~成功，2~失败;type:smallint;default:0"`
	CreatedAt    time.Time `gorm:"comment:创建时间"`
	UpdatedAt    time.Time `gorm:"comment:更新时间"`
}

// 事务性消息
type NsqMessage struct {
	ID          uint64       `gorm:"comment:自增主键;primaryKey;type:bigserial;not null"`
	Topic       string       `gorm:"comment:topic;size:36;not null"`
	MsgKey      string       `gorm:"comment:消息唯一标识;size:36;not null"`
	Payload     orm.JSON     `gorm:"comment:消息内容"`
	Status      uint8        `gorm:"comment:消息状态， 0~初始，1~成功，2~标记成功;type:smallint;not null;default:0"`
	ExecuteTime sql.NullTime `gorm:"comment:下一次执行时间;index:IDX_PROD_NSQ_EXECUTE"`
	Retry       uint32       `gorm:"comment:重试次数;type:integer;not null;default:0"`
	CreatedAt   time.Time    `gorm:"comment:创建时间"`
	UpdatedAt   time.Time    `gorm:"comment:更新时间"`
}
